package com.chen.palmar.project.producer;

import android.os.Bundle;
import android.widget.TextView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseFragment;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.entity.ProducerBaseEntity;

import java.util.HashMap;

import butterknife.Bind;
import rx.Subscription;

/**
 * Created by chen on 2018/1/6.
 */

public class BaseInfoFragment extends BaseFragment {

    @Bind(R.id.tv_base_info)
    TextView tvBaseInfo;
    @Bind(R.id.tv_base_name)
    TextView tvBaseName;
    @Bind(R.id.tv_base_contract)
    TextView tvBaseContract;
    @Bind(R.id.tv_base_phone)
    TextView tvBasePhone;
    @Bind(R.id.tv_base_tag)
    TextView tvBaseTag;
    @Bind(R.id.tv_base_store)
    TextView tvBaseStore;
    @Bind(R.id.tv_base_address)
    TextView tvBaseAddress;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_base;
    }

    @Override
    public void initData() {
        try {
            getDataForWeb(getArguments().getString("info"));
        } catch (Exception e) {
            e.printStackTrace();
            showToast("服务器数据出错，请稍后重试");
        }
    }

    private void getDataForWeb(String info) throws Exception{
        HashMap<String, String> map = new HashMap<>();
        map.put("factoryId", info);
        Subscription subscribe = DataCenter.producerBaseInfo(map).subscribe(new HttpSubscriber<ProducerBaseEntity>(getContext(), null) {
            @Override
            public void onNext(ProducerBaseEntity entity) {
                ProducerBaseEntity.DataBean data = entity.getData();
                tvBaseAddress.setText(data.getAddress_detail());
                tvBaseContract.setText(data.getContacts());
                tvBaseInfo.setText(data.getDesc());
                tvBasePhone.setText(data.getMobile());
                tvBaseName.setText(data.getName());
                tvBaseStore.setText(data.getProductName());
                tvBaseTag.setText(data.getBrand());
            }
        });
        subscription.add(subscribe);
    }
}
